package com.iot08.api.service;

import com.iot08.api.constant.Constant;
import com.iot08.api.vo.request.base.BaseAccountReqVO;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.app.AdverstersComInfo;
import com.iot08.common.entity.app.Advert;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.PackageTools;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/1/4 11:09
 */
@Slf4j
@Service
public class AdvertService extends BaseService<Advert> {

    @Resource
    private AdverstersComInfoService adverstersComInfoService;

    //免广告
    @Value("${ad.app.audit.account:2c312c229d5d523aad6a68ef}")
    private String AD_APP_AUDIT_ACCOUNT;

    /**
     * 获取自己的广告配置接口
     *
     * @param vo
     * @time: 2021/1/4 14:52
     * @author: Rock
     * @version 1.0
     */
    public ResponseMap getInfo(BaseAccountReqVO vo) {
        ResponseMap res = ResponseMap.success();
        //判断是否是的审核账号,如果是,则 返回正常code:0 且空数据的就行
        AdverstersComInfo info = adverstersComInfoService.findById("main_info");
        if ((info != null && StringUtil.isNotBlank(info.getAppAuditAccount())
                && info.getAppAuditAccount().equals(vo.getOpenid())) || AD_APP_AUDIT_ACCOUNT.equals(vo.getOpenid())) {
            //是审核账号,返回正常code:0 且空数据
            return ResponseMap.success();
        }
        boolean isMajia = false;
        //判断是否是马甲包 (e路相伴)
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //获取不区分大小写, 统一小写
        String userAgent = req.getHeader("user-agent");
        String bundleid = req.getHeader("Bundleid");
        int requestTypeWithUserAgent = PackageTools.getRequestTypeWithUserAgent(userAgent, bundleid);
        if (requestTypeWithUserAgent == 101 || requestTypeWithUserAgent == 201) {
            //其他马甲包(iOS其他马甲包:101  安卓其他马甲包:201)
            isMajia = true;
        }

        //show_app_type : 0(默认):e路相伴  1:赛诺
        Query query = new Query(Criteria.where("status").is(1).and("deleted").is(0));
        List<Advert> advertList = this.find(query.with(Sort.by(Sort.Order.asc("index"))));
        List<Map<String, Object>> slideshow = new ArrayList<>();
        List<Map<String, Object>> serviceZone = new ArrayList<>();
        List<Map<String, Object>> homeList = new ArrayList<>();
        List<Map<String, Object>> chatList = new ArrayList<>();
        List<Map<String, Object>> locateList = new ArrayList<>();
        for (Advert advert : advertList) {
            if (isMajia) {
                // 马甲包: e路相伴 的 不是其类型的不显示 跳过
                if(advert.getShowAppType() != 0){
                    continue;
                }
            }else{
                // 赛诺 的 不是其类型的不显示 跳过
                if(advert.getShowAppType() != 1){
                    continue;
                }
            }
            Map<String, Object> temp = new HashMap<>();
            skipTypeHandle(advert, temp);
            if (advert.getType() == -2) {
                //悬浮媒体位
                temp.put("id", advert.getNumber() + "");
                temp.put("image", advert.getImgUrl());
                temp.put("title", advert.getTitle());
                temp.put("url", advert.getLink());
                if (isMajia && advert.getMajia_link() != null){
                    temp.put("url", advert.getMajia_link());
                }
                res.put("suspensionButtonData", temp);
            } else if (advert.getType() == -1) {
                //首页插屏媒体位
                temp.put("id", advert.getNumber() + "");
                temp.put("url", advert.getLink());
                temp.put("title", advert.getTitle());
                temp.put("vertical_prompt_image", advert.getImgUrl());
                res.put("bannerPromptTips", temp);
            } else if (advert.getType() == 1) {
                //首页banner 轮播图
                temp.put("id", advert.getId());
                temp.put("title", advert.getTitle());
                temp.put("image", advert.getImgUrl());
                slideshow.add(temp);
            } else if (advert.getType() == 2) {
                //贴心服务专区
                temp.put("id", advert.getId());
                temp.put("image", advert.getImgUrl());
                serviceZone.add(temp);
            } else if (advert.getType() >= 3) {
                //页面文字滚动 3 = 首页文字；4 = 微聊文字；5 = 定位文字
                temp.put("id", advert.getId());
                temp.put("title", advert.getTitle());

                Map<String, Object> tempMap = new HashMap<>();
                if (advert.getType() == 3) {
                    temp.put("pageIndex", "home");
                    homeList.add(temp);
                } else if (advert.getType() == 4) {
                    temp.put("pageIndex", "chat");
                    chatList.add(temp);
                } else if (advert.getType() == 5) {
                    temp.put("pageIndex", "locate");
                    locateList.add(temp);
                }
            }
        }
        if (slideshow.size() > 0) {
            res.put("slideshow", slideshow);
        }
        if (serviceZone.size() > 0) {
            res.put("serviceZone", serviceZone);
        }
        Map<String, Object> pageTips = new HashMap<>();
        if (homeList.size() > 0) {
            pageTips.put("home", homeList);
        }
        if (chatList.size() > 0) {
            pageTips.put("chat", chatList);
        }
        if (locateList.size() > 0) {
            pageTips.put("locate", locateList);
        }
        if (pageTips.size() > 0) {
            res.put("pageTips", pageTips);
        }
        return res;
    }

    /**
     * @param advert
     * @param temp
     * @time: 2021/1/4 11:50
     * @author: Rock
     * @version 1.0
     */
    private void skipTypeHandle(Advert advert, Map<String, Object> temp) {
        if (Const.INT_1.equals(advert.getSkipType())) {
            temp.put("type", "web");
            if (StringUtil.isNotBlank(advert.getLink())) {
                temp.put("url", advert.getLink());
            }
        } else if (new Integer(2).equals(advert.getSkipType())) {
            temp.put("type", "small_program");
            temp.put("smallprogram_id", advert.getSmallProgramId());
            temp.put("smallprogram_path", advert.getSmallProgramPath());
        } else if (new Integer(3).equals(advert.getSkipType())) {
            temp.put("type", "web_jump_WXGZ");
            temp.put("bottom_btn_image", advert.getBottomBtnImage());
            temp.put("include_QRcode_image", advert.getIncludeQRcodeImage());
            temp.put("jump_Tips_Words", advert.getJumpTipsWords());
            temp.put("url", advert.getLink());
        } else if (new Integer(5).equals(advert.getSkipType())) {
            temp.put("type", "deeplink");
            if (StringUtil.isNotBlank(advert.getLink())) {
                temp.put("url", advert.getLink());
            }
        } else {
            temp.put("type", "ADSDK_jump");
            temp.put("SDKType", advert.getAdvertFirm());
        }
    }
}
